1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Data;
5 using
System.Drawing;
6 using
System.Linq;
7 using
System.Text;
8 using
System.Windows.Forms;
9 using
System.Data.SqlClient;
10 namespace
WarehouseManagementSystem
11 {
12     
public partial class frmRegistration : Form
13     {
14         SqlDataReader rdr =
null;
15         DataTable dtable =
new DataTable();
16         SqlConnection con =
null;
17         SqlCommand cmd =
null;
18         DataTable dt =
new DataTable();
19         ConnectionString cs =
new ConnectionString();
20         
public frmRegistration()
21         {
22             InitializeComponent();
23         }
24
25         
private void Form1_Load(object sender, EventArgs e)
26         {
27
28             Autocomplete();
29         }
30         
private void Reset()
31         {
32             txtUsername.Text =
"";
33             cmbUserType.SelectedIndex = -
1;
34             txtPassword.Text =
"";
35             txtContact_no.Text =
"";
36             txtName.Text =
"";
37             txtEmail_Address.Text =
"";
38             btnRegister.Enabled =
true;
39             btnDelete.Enabled =
false;
40             btnUpdate_record.Enabled =
false;
41             txtUsername.Focus();
42         }
43         
private void NewRecord_Click(object sender, EventArgs e)
44         {
45             Reset();
46         }
47
48         
private void Register_Click(object sender, EventArgs e)
49         {
50             
if (txtUsername.Text == "")
51             {
52                 MessageBox.Show(
"Please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
53                 txtUsername.Focus();
54                 
return;
55             }
56             
if (cmbUserType.Text == "")
57             {
58                 MessageBox.Show(
"Please select user type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
59                 cmbUserType.Focus();
60                 
return;
61             }
62             
if (txtPassword.Text == "")
63             {
64                 MessageBox.Show(
"Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
65                 txtPassword.Focus();
66                 
return;
67             }
68             
if (txtName.Text == "")
69             {
70                 MessageBox.Show(
"Please enter name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
71                 txtName.Focus();
72                 
return;
73             }
74             
if (txtContact_no.Text == "")
75             {
76                 MessageBox.Show(
"Please enter contact no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
77                 txtContact_no.Focus();
78                 
return;
79             }
80             
if (txtEmail_Address.Text == "")
81             {
82                 MessageBox.Show(
"Please enter email", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
83                 txtEmail_Address.Focus();
84                 
return;
85             }
86             
try
87             {
88                 con =
new SqlConnection(cs.DBConn);
89                 con.Open();
90                 
string ct = "select username from registration where username=@find";
91
92                 cmd =
new SqlCommand(ct);
93                 cmd.Connection = con;
94                 cmd.Parameters.Add(
new SqlParameter("@find", System.Data.SqlDbType.NChar, 30, "username"));
95                 cmd.Parameters[
"@find"].Value = txtUsername.Text;
96                 rdr = cmd.ExecuteReader();
97
98                 
if (rdr.Read())
99                 {
100                     MessageBox.Show(
"Username Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
101                     txtUsername.Text =
"";
102                     txtUsername.Focus();
103
104
105                     
if ((rdr != null))
106                     {
107                         rdr.Close();
108                     }
109                     
return;
110                 }
111                 con =
new SqlConnection(cs.DBConn);
112                 con.Open();
113                 
string ct1 = "select Email from registration where Email='" + txtEmail_Address.Text + "'";
114
115                 cmd =
new SqlCommand(ct1);
116                 cmd.Connection = con;
117                 rdr = cmd.ExecuteReader();
118
119                 
if (rdr.Read())
120                 {
121                     MessageBox.Show(
"Email ID Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
122                     txtEmail_Address.Text =
"";
123                     txtEmail_Address.Focus();
124
125
126                     
if ((rdr != null))
127                     {
128                         rdr.Close();
129                     }
130                     
return;
131                 }
132                 con =
new SqlConnection(cs.DBConn);
133                 con.Open();
134
135                 
string cb = "insert into Registration(Username,UsertYpe,Password,ContactNo,Email,Name,JoiningDate) VALUES ('" + txtUsername.Text + "','" + cmbUserType.Text + "','" + txtPassword.Text + "','" + txtContact_no.Text + "','" + txtEmail_Address.Text + "','" + txtName.Text + "','" + System.DateTime.Now + "')";
136
137                 cmd =
new SqlCommand(cb);
138                 cmd.Connection = con;
139                 cmd.ExecuteReader();
140                 con.Close();
141                 MessageBox.Show(
"Successfully Registered", "User", MessageBoxButtons.OK, MessageBoxIcon.Information);
142                 Autocomplete();
143                 btnRegister.Enabled =
false;
144
145             }
146             
catch (Exception ex)
147             {
148                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
149             }
150         }
151
152        
153         
private void CheckAvailability_Click(object sender, EventArgs e)
154         {
155             
try
156             {
157                 
if (txtUsername.Text == "")
158                 {
159                     MessageBox.Show(
"Please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
160                     txtUsername.Focus();
161                     
return;
162                 }
163                 con =
new SqlConnection(cs.DBConn);
164                 con.Open();
165                 
string ct = "select username from registration where username='" + txtUsername.Text + "'";
166
167                 cmd =
new SqlCommand(ct);
168                 cmd.Connection = con;
169                 rdr = cmd.ExecuteReader();
170
171                 
if (rdr.Read())
172                 {
173                     MessageBox.Show(
"Username not available", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
174                     
return;
175                 }
176                 
if (!rdr.Read())
177                 {
178                     MessageBox.Show(
"Username available", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
179                     txtUsername.Focus();
180
181                 }
182                 
if ((rdr != null))
183                 {
184                     rdr.Close();
185                 }
186             }
187             
catch (Exception ex)
188             {
189                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
190             }
191         }
192
193         
private void Email_Address_Validating(object sender, CancelEventArgs e)
194         {
195             System.Text.RegularExpressions.Regex rEMail =
new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
196             
if (txtEmail_Address.Text.Length > 0)
197             {
198                 
if (!rEMail.IsMatch(txtEmail_Address.Text))
199                 {
200                     MessageBox.Show(
"invalid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
201                     txtEmail_Address.SelectAll();
202                     e.Cancel =
true;
203                 }
204             }
205         }
206
207         
private void Name_Of_User_KeyPress(object sender, KeyPressEventArgs e)
208         {
209             e.Handled = !(
char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space);
210         }
211
212         
private void Username_Validating(object sender, CancelEventArgs e)
213         {
214             System.Text.RegularExpressions.Regex rEMail =
new System.Text.RegularExpressions.Regex("^[a-zA-Z0-9_]");
215             
if (txtUsername.Text.Length > 0)
216             {
217                 
if (!rEMail.IsMatch(txtUsername.Text))
218                 {
219                     MessageBox.Show(
"only letters,numbers and underscore is allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
220                     txtUsername.SelectAll();
221                     e.Cancel =
true;
222                 }
223             }
224         }
225
226         
private void GetDetails_Click(object sender, EventArgs e)
227         {
228             frmRegisteredUsersDetails frm =
new frmRegisteredUsersDetails();
229             frm.dataGridView1.DataSource = frm.GetData();
230             frm.Show();
231
232         }
233
234         
private void Username_TextChanged(object sender, EventArgs e)
235         {
236
237             btnDelete.Enabled =
true;
238             btnUpdate_record.Enabled =
true;
239             
try
240             {
241                 txtUsername.Text = txtUsername.Text.TrimEnd();
242                 con =
new SqlConnection(cs.DBConn);
243
244                 con.Open();
245                 cmd = con.CreateCommand();
246
247                 cmd.CommandText =
"SELECT Usertype,password,name,contactno,email FROM registration WHERE username = '" + txtUsername.Text.Trim() + "'";
248                 rdr = cmd.ExecuteReader();
249
250                 
if (rdr.Read())
251                 {
252                     cmbUserType.Text = (rdr.GetString(
0).Trim());
253                     txtPassword.Text = (rdr.GetString(
1).Trim());
254                     txtName.Text = (rdr.GetString(
2).Trim());
255                     txtContact_no.Text = (rdr.GetString(
3).Trim());
256                     txtEmail_Address.Text = (rdr.GetString(
4).Trim());
257                 }
258
259                 
if ((rdr != null))
260                 {
261                     rdr.Close();
262                 }
263                 
if (con.State == ConnectionState.Open)
264                 {
265                     con.Close();
266                 }
267
268
269             }
270             
catch (Exception ex)
271             {
272                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
273             }
274         }
275
276         
private void Autocomplete()
277         {
278             
try{
279             con =
new SqlConnection(cs.DBConn);
280             con.Open();
281             SqlCommand cmd =
new SqlCommand("SELECT username FROM registration", con);
282             DataSet ds =
new DataSet();
283             SqlDataAdapter da =
new SqlDataAdapter(cmd);
284             da.Fill(ds,
"registration");
285             AutoCompleteStringCollection col =
new AutoCompleteStringCollection();
286             
int i = 0;
287             
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
288             {
289                 col.Add(ds.Tables[
0].Rows[i]["Username"].ToString());
290
291             }
292             txtUsername.AutoCompleteSource = AutoCompleteSource.CustomSource;
293             txtUsername.AutoCompleteCustomSource = col;
294             txtUsername.AutoCompleteMode = AutoCompleteMode.Suggest;
295
296             con.Close();
297             }
298             
catch (Exception ex)
299             {
300                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
301             }
302         }
303
304         
private void Update_record_Click(object sender, EventArgs e)
305         {
306             
if (txtUsername.Text == "")
307             {
308                 MessageBox.Show(
"Please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
309                 txtUsername.Focus();
310                 
return;
311             }
312             
if (cmbUserType.Text == "")
313             {
314                 MessageBox.Show(
"Please select user type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
315                 cmbUserType.Focus();
316                 
return;
317             }
318             
if (txtPassword.Text == "")
319             {
320                 MessageBox.Show(
"Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
321                 txtPassword.Focus();
322                 
return;
323             }
324             
if (txtName.Text == "")
325             {
326                 MessageBox.Show(
"Please enter name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
327                 txtName.Focus();
328                 
return;
329             }
330             
if (txtContact_no.Text == "")
331             {
332                 MessageBox.Show(
"Please enter contact no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
333                 txtContact_no.Focus();
334                 
return;
335             }
336             
if (txtEmail_Address.Text == "")
337             {
338                 MessageBox.Show(
"Please enter email", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
339                 txtEmail_Address.Focus();
340                 
return;
341             }
342             
try
343             {
344                 con =
new SqlConnection(cs.DBConn);
345                 con.Open();
346
347                 
string cb = "update registration set usertype='" + cmbUserType.Text + "', password='" + txtPassword.Text + "',contactno='" + txtContact_no.Text + "',email='" + txtEmail_Address.Text + "',name='" + txtName.Text + "' where username='" + txtUsername.Text + "'";
348                 cmd =
new SqlCommand(cb);
349                 cmd.Connection = con;
350                 cmd.ExecuteReader();
351                 con.Close();
352         
353                 MessageBox.Show(
"Successfully updated", "User Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
354                 Autocomplete();
355                 btnUpdate_record.Enabled =
false;
356             }
357             
catch (Exception ex)
358             {
359                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
360             }
361         }
362
363         
private void Delete_Click(object sender, EventArgs e)
364         {
365             
if (MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
366             {
367                 delete_records();
368             }
369         }
370         
private void delete_records()
371         {
372
373             
try
374             {
375                 
if (txtUsername.Text == "admin")
376                 {
377                     MessageBox.Show(
"Admin Account can not be deleted", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
378                     
return;
379                 }
380                 
int RowsAffected = 0;
381                 con =
new SqlConnection(cs.DBConn);
382                 con.Open();
383                 
string ct = "delete from Registration where Username='" + txtUsername.Text + "'";
384                 cmd =
new SqlCommand(ct);
385                 cmd.Connection = con;
386                 RowsAffected = cmd.ExecuteNonQuery();
387                 
if (RowsAffected > 0)
388                 {
389                     MessageBox.Show(
"Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
390                     Autocomplete();
391                     Reset();
392                 }
393                 
else
394                 {
395                     MessageBox.Show(
"No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information);
396                     Reset();
397                     Autocomplete();
398                 }
399                 
if (con.State == ConnectionState.Open)
400                 {
401                     con.Close();
402                 }
403              
404             }
405             
catch (Exception ex)
406             {
407                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
408             }
409         }
410
411       
412         
private void txtContact_no_KeyPress(object sender, KeyPressEventArgs e)
413         {
414             
if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
415             {
416                 e.Handled =
false;
417             }
418             
else
419             {
420                 e.Handled =
true;
421             }
422         }
423
424      
425     }
426 }


Gõ tìm kiếm nhanh...